package com.reebake.ideal.security.oauth2.client;

import com.reebake.ideal.servlet.core.ResponseBodyWrapper;
import com.reebake.ideal.servlet.util.WebUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.web.DefaultRedirectStrategy;

import java.io.IOException;

@RequiredArgsConstructor
public class SmartOauth2RedirectStrategy extends DefaultRedirectStrategy {
	private final ResponseBodyWrapper responseBodyWrapper;

	@Override
	public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
		if(WebUtil.isAjaxRequest(request)) {
			responseBodyWrapper.convertAndWrite(response, url);
		}else {
			super.sendRedirect(request, response, url);
		}
	}
}
